<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  각 멤버 함수 호출시 부트스트랩 html리턴.
**************************************************************************** */

class Nhtml {

	private $CI;

    public function __construct() {
		$this->CI =& get_instance();
    }

	/***************************************************
	 * Najax (form Element binding)
	 ***************************************************/
	private $nameValues = array();

	public function setNameValues($value01){
		array_push( $this->nameValues, $value01);
	}

	public function getNameValues(){
		return nPrt($this->nameValues);
	}

	public function getAjaxNameValues($arg01) {
		$retVal = '';
		$nameValues01 = '';
		foreach ($this->nameValues as $key => $val) {
			if($key != 0){
				$nameValues01 .= sprintf(',%s',nPhpEol());
			}
			$nameValues01 .= sprintf('\'%s\'', $val);
			}
			$retVal = sprintf('var nameValuesPref = [%s];%s', $nameValues01, nPhpEol());
		return $retVal;
	}

	/***************************************************
	 * container
	 ***************************************************/
	public function paginationContainer(){
		return sprintf('<div class="col-md-12 pull-right text-right nPagination"></div>');
	}

	public function tableContainer($argArray = array()){
		$th = '';
		foreach($argArray['th'] as $key=>$val){
			$th .= sprintf('<th>%s</th>', $val);
		}
		$cols = '';
		foreach($argArray['cols'] as $key=>$val){
			if(empty($val) !== FALSE){
				$cols .= sprintf('<col>');
			} else {
				$cols .= sprintf('<col style="%s">', $val);
			}
		}
		$retVal = sprintf('
			<div class="table-responsive">
				<table class="table table-hover table-bordered table-condensed nTable %s">
					<colgroup>
						%s
					</colgroup>
					<thead>
						<tr>
							%s
						</tr>
					</thead>
					<tbody>
					</tbody>
				</table>
			</div>',
			@$argArray['uId'], $cols, $th
		);
		return $retVal;
	}


	/***************************************************
	 * PageHeader
	 ***************************************************/
	public function nHeader($argArray = array()){
		$retVal = sprintf('
			<div class="header">
				<h1 class="page-title">%s</h1>
				<h2 class="page-subject">%s</h2>
			</div>',
			$argArray[0],$argArray[1]
		);
		return $retVal;

	}
	public function nSubjection($argText){
		$retVal = sprintf('<h3>%s</h3>', $argText);
		return $retVal;
	}


	/***************************************************
	 * Separation
	 ***************************************************/
	public function nHr($argArray = array()){
		$css = "";
		if(empty($argArray['blank'])!==FALSE){
			$css = "col-sm-12 padl0 padr0";
		}
		return sprintf('<hr %s/>', 'class="'.$css.'"');
	}

	public function nSeparatorHorizontal(){
		return '<div class="nSeparatorHorizontal"></div>';
	}


	/***************************************************
	 * FormElement
	 ***************************************************/
	public function getCreateOnly($mode){
		return $mode=='create'?FALSE:TRUE;
	}

	public function nFormOpen($action = '', $attributes = '', $hidden = array()) {
		if ($attributes == '') {
			$attributes = 'method="post"';
		}
		$action = $action;
		$form = '<form action="'.$action.'"';
		$form .= _attributes_to_string($attributes, TRUE);
		$form .= '>';
		if ($this->CI->config->item('csrf_protection') === TRUE) {
			$hidden[$this->CI->security->get_csrf_token_name()] = $this->CI->security->get_csrf_hash();
		}
		if (is_array($hidden) AND count($hidden) > 0) {
			$form .= sprintf("<div style=\"display:none\">%s</div>", form_hidden($hidden));
		}
		return $form;
	}

	public function nFormClose(){
		return '</form>';
	}

	public function nHidden($argArray = array()){
		$this->setNameValues($this->CI->nurl->encrypt($argArray["id"]));
		$retVal = sprintf('
			<input type="%s" id="%s" name="%s" value="%s">
			',
			@$argArray['type'], $this->CI->nurl->encrypt($argArray["id"]), $this->CI->nurl->encrypt($argArray["id"]), @$argArray['defaultValue']
		);
		return $retVal;
	}

	public function nInput($argArray = array()){
		$readonly = '';
		$disabledInputAddOn = '';
		if(@$argArray['createOnly'] == FALSE ){
			$this->setNameValues($this->CI->nurl->encrypt($argArray["id"]));
		} else {
			$readonly = 'readonly="readonly"';
			$disabledInputAddOn = 'disabled';
		}
		$maxlength = 'maxlength="255"';
		if(empty($argArray['maxlength']) === FALSE){
			$maxlength = sprintf('maxlength="%s"', @$argArray['maxlength']);
		}
		if(empty($argArray['type']) === FALSE && $argArray['type'] == 'number'){
			$maxlength = 'pattern="\d*" maxlength="10"';
		}
		$retVal = sprintf('
			<div class="nInput form-group nValidation">
				<label for="%s" class="control-label col-sm-2">%s</label>
				<div class="input-group">
					<input type="text" class="form-control" id="%s" name="%s" placeholder="%s" value="%s" %s %s>
					<span class="input-group-addon input-group %s">%s</span>
				</div>
			</div>',
			$this->CI->nurl->encrypt($argArray["id"]), @$argArray['title'],
			$this->CI->nurl->encrypt($argArray["id"]),
			$this->CI->nurl->encrypt($argArray["id"]), @$argArray['placeholder'], @$argArray['defaultValue'], $readonly, $maxlength,
			$disabledInputAddOn,@$argArray['labelText']
		);
		return $retVal;
	}

	public function nDateTimeInput($argArray = array()){
		$readonly = '';
		$datepicker = '';
		$addOn = '';
		$dateReadOnly = 'nDate';
		$timeReadOnly = 'nTime';
		$disabledInputAddOn = '';
		if(@$argArray['createOnly'] == FALSE ){
			$this->setNameValues($this->CI->nurl->encrypt($argArray["idYMD"]));
			$this->setNameValues($this->CI->nurl->encrypt($argArray["idHIS"]));
		} else {
			$readonly = 'readonly="readonly"';
			$dateReadOnly = '';
			$timeReadOnly = '';
			$disabledInputAddOn = 'disabled';
		}
		if($argArray['type'] == 'datetime'){
			$argArray['type'] = 'text';
			$datepicker = 'datetime';
			$addOn ='';
			//$dateReadOnly = 'readonly="readonly"';
		} else {
			$argArray['type'] = $argArray['type'];

		}

		$retVal = sprintf( '
			<div class="nInput form-group nValidation">
				<label for="%s" class="control-label col-sm-2">%s</label>
				<div class="input-group %s">
					<input type="%s" class="form-control %s" id="%s" name="%s" placeholder="%s" value="%s" %s maxlength="10">
					<span class="input-group-addon input-group %s">%s</span>
					<input type="%s" class="form-control %s" id="%s" name="%s" placeholder="%s" value="%s" %s maxlength="8">
					<span class="input-group-addon input-group %s">%s</span>
				</div>
			</div>',
			$this->CI->nurl->encrypt($argArray["idYMD"]), @$argArray['title'],
			$datepicker,
			@$argArray['type'], $dateReadOnly, $this->CI->nurl->encrypt($argArray["idYMD"]), $this->CI->nurl->encrypt($argArray["idYMD"]), @$argArray['placeholderYMD'], empty($argArray['defaultValueYMD']) !== TRUE ? $argArray['defaultValueYMD'] : getNowDate(), $readonly,
			$disabledInputAddOn,@$argArray['labelTextYMD'],
			@$argArray['type'], $timeReadOnly, $this->CI->nurl->encrypt($argArray["idHIS"]), $this->CI->nurl->encrypt($argArray["idHIS"]), @$argArray['placeholderHIS'], empty($argArray['defaultValueHIS']) !== TRUE ? $argArray['defaultValueHIS'] : getNowTime(), $readonly,
			$disabledInputAddOn, @$argArray['labelTextHIS']
		);
		return $retVal;
	}

	public function nTextarea($argArray = array()){
		$readonly = '';
		$disabledInputAddOn = '';
		if(@$argArray['createOnly'] == FALSE ){
			$this->setNameValues($this->CI->nurl->encrypt($argArray["id"]));
		} else {
			$readonly = 'readonly="readonly"';
			$disabledInputAddOn = 'disabled';
		}
		$retVal = sprintf( '
			<div class="nTextArea form-group nValidation">
				<label for="%s" class="control-label col-sm-2">%s</label>
				<div class="input-group">
					<textarea class="form-control" id="%s" name="%s" placeholder="%s" rows="5" %s>%s</textarea>
					<span class="input-group-addon input-group %s">%s</span>
				</div>
			</div>',
			@$this->CI->nurl->encrypt($argArray["id"]), @$argArray['title'],
			@$this->CI->nurl->encrypt($argArray["id"]), @$this->CI->nurl->encrypt($argArray["id"]), @$argArray['placeholder'], $readonly, @$argArray['defaultValue'],
			$disabledInputAddOn, @$argArray['labelText']
		);
		return $retVal;
	}

	public function nCheckbox($argArray = array()){
		$readonly = '';
		if(@$argArray['createOnly'] == FALSE ){
			$this->setNameValues($this->CI->nurl->encrypt($argArray["id"]));
		}
		$mDom = '';
		foreach(@$argArray['items'] as $key=>$val){
			$defaultValue = '';
			$defaultValueActive = '';
			if($argArray['defaultValue'] == $key){
				 $defaultValueActive = 'active';
			}
			if(@$argArray['createOnly'] == FALSE ){
				$curtain = '';
			} else {
				$curtain = '<div style="position:relative"><div style="background-color:transparent;width:100%;height:34px;position:absolute;z-index:9999;"></div></div>';
			}
			$mDom .= sprintf('
				<label class="btn btn-default %s" >
					<input type="checkbox" class="checkbox" name="%s" value="%s" %s > %s
				</label>',
				$defaultValueActive,
				@$this->CI->nurl->encrypt($argArray["id"]), $key, $defaultValue, $val
			);
		}
		$retVal = sprintf( '
			<div class="nCheckbox form-group nValidation">
				%s
				<label class="control-label col-sm-2">%s</label>
				<div class="btn-group" data-toggle="buttons">
					%s
				</div>
			</div>',
			$curtain,
			@$argArray['title'],
			$mDom
		);
		return $retVal;
	}

	public function nRadio($argArray = array()){
		$readonly = '';
		if(@$argArray['createOnly'] == FALSE ){
			$this->setNameValues($this->CI->nurl->encrypt($argArray["id"]));
		}
		if(@$argArray['createOnly'] == FALSE ){
			$curtain = '';
		} else {
			$curtain = '<div style="position:relative"><div style="background-color:transparent;width:100%;height:34px;position:absolute;z-index:9999;"></div></div>';
		}
		$mDom = '';
		foreach(@$argArray['items'] as $key=>$val){
			$defaultValue = '';
			$defaultValueActive = '';
			if($argArray['defaultValue'] == $key){
				 $defaultValueActive = 'active';
				 $defaultValue = 'checked="checked"';
			}
			$mDom .= sprintf('
				<label class="btn btn-default %s">
					<input type="radio" name="%s" id="%s" value="%s" %s> %s
				</label>',
				$defaultValueActive,
				@$this->CI->nurl->encrypt($argArray["id"]), @$this->CI->nurl->encrypt($argArray["id"]).'-'.$key, $key, $defaultValue, $val
			);
		}
		$retVal = sprintf( '
			<div class="nRadio form-group nValidation">
				%s
				<label class="control-label col-sm-2">%s</label>
				<div class="btn-group" data-toggle="buttons">
					%s
				</div>
			</div>',
			$curtain,
			@$argArray['title'],
			$mDom
		);
		return $retVal;
	}

	public function nSelect($argArray = array()){
		if(@$argArray['createOnly'] == FALSE ){
			$this->setNameValues($this->CI->nurl->encrypt($argArray["id"]));
		}
		if(@$argArray['createOnly'] == FALSE ){
			$curtain = '';
		} else {
			$curtain = '<div style="position:relative"><div style="background-color:transparent;width:100%;height:34px;position:absolute;z-index:9999;"></div></div>';
		}
		$mDom = '';
		if(@is_array($argArray['group'])){
			foreach($argArray['group'] as $keygroup => $valgroup){
				$mDom .= sprintf('
					<optgroup label="%s">',
					$valgroup['outgroup']
				);
				foreach($valgroup['items'] as $keyItem => $valItem){
					$defaultValue = '';
					if($argArray['defaultValue'] == $keyItem){
						$defaultValue = "selected='selected'";
					}
					$mDom .= sprintf('<option value="%s" %s>%s</option>',
						$keyItem, $defaultValue, $valItem
					);
				}
				$mDom .= sprintf('</optgroup>');
			}
		}
		if(@is_array($argArray['items'])) {
			foreach($argArray['items'] as $keyItem => $valItem){
				$defaultValue = '';
				if($argArray['defaultValue'] == $keyItem){
					$defaultValue = "selected='selected'";
				}

				$mDom .= sprintf('<option value="%s" %s>%s</option>',
					$keyItem, $defaultValue, $valItem
				);
			}
		}
		$retVal = sprintf( '
			<div class="nSelect form-group nValidation col-sm-12 padl0 padr0">
				%s
				<label for="fg_auth_type" class="control-label col-sm-2">%s</label>
				<div class="row-fluid col-sm-10 padl0 padr0">
					<select class="selectpicker" name="%s">
						%s
					</select>
				</div>
			</div>',
			$curtain,
			@$argArray['title'],
			@$this->CI->nurl->encrypt($argArray["id"]),
			$mDom
		);
		return $retVal;
	}


	/***************************************************
	 * ButtonsElement
	 ***************************************************/
	public function nButtons($argArray = array()){
		$mDom = '';
		$retVal = '';

		$mDom .= sprintf('%s<div class="nButtonHorizontal form-group">%s',nPhpEol(),nPhpEol());
		$mDom .= sprintf('<span class="control-label col-sm-2"></span>%s',nPhpEol());
			if($argArray['layout'] == 'horizontal') $mDom .= sprintf('<div class="btn-group">%s',nPhpEol());
			if($argArray['layout'] == 'horizontal-justified') $mDom .= sprintf('<div class="btn-group btn-group-justified nBtnGroup">%s',nPhpEol());

			foreach($argArray['items'] as $item){
				if($argArray['layout'] == 'vertical') $mDom .= sprintf('<div class="btn-group btn-group-justified nBtnGroup">%s',nPhpEol());
				if($item['type'] == 'primary'){
					$iconType = "fa fa-check";
				} else {
					$iconType = "fa fa-ban";
				}
				$mDom .= sprintf('
					<a href="javascript:void(0)" class="btn btn-%s %s %s">
						<span class="%s"></span> %s
					</a>
					',
					$item['type'], $item['class'], ($argArray['layout'] == 'vertical'?'nTextLeft':''),
					$iconType, $item['text']
				);
				if($argArray['layout'] == 'vertical') $mDom .= sprintf('</div>%s',nPhpEol());
			}

			if($argArray['layout'] == 'horizontal') $mDom .= sprintf('</div>%s',nPhpEol());
			if($argArray['layout'] == 'horizontal-justified') $mDom .= sprintf('</div>%s',nPhpEol());

		$mDom .= sprintf('</div>');
		$retVal .= $mDom;

		return $retVal;
	}

	public function nBtnToolbarOpen($argArray = array()){
		$pullLeft = ($argArray['size'] == '12') ? '' : 'pull-left';
		$retVal = sprintf('<div class="btn-toolbar col-sm-%s %s list-toolbar">', $argArray['size'], $pullLeft);
		return $retVal;
	}

	public function nBtnToolbarClose(){
		return '</div>';
	}

	function nToolbarButton($argArray = array()){
		$retVal = sprintf('
			<a href="javascript:%s" class="btn btn-%s %s">
				<i class="%s"></i> %s
			</a>',
			$argArray['link'], $argArray['type'], $argArray['class'],
			$argArray['icon'], $argArray['text']
		);
		return $retVal;
	}



	/***************************************************
	 * TextBox
	 ***************************************************/

	 function nTextBox($argArray = array()){
		$retVal = sprintf('
			<div class="nRadio form-group">
				<label class="control-label col-sm-2">%s</label>
				<div class="input-group">
					<div class="well">
						%s
					</div>
					<span class="input-group-addon input-group wellFriend"></span>
				</div>
			</div>',
			$argArray['title'], $argArray['content']
		);
		return $retVal;
	 }

	 function nLabel($argArray = array()){
		$retVal = sprintf('
			<div class="nRadio form-group">
				<label class="control-label col-sm-2">%s</label>
				<div class="input-group">
					<div class="well well2">
						%s
					</div>
					<span class="input-group-addon input-group wellFriend"></span>
				</div>
			</div>',
			$argArray['title'], $argArray['content']
		);
		return $retVal;
	 }




	/***************************************************
	 * 검색박스
	 ***************************************************/
	 function nSearchBarAType($argArray = array()){
		$option ='';
		foreach($argArray['optionItems'] as $key=>$val){
			$option .= sprintf('<option value="%s">%s</option>',
				$this->CI->nurl->encrypt($key), $val
			);
		}
		$retVal = sprintf('
			<div class="fr">
				<div class="form-group fl marr3 ">
					<select class="form-control borderr1 curp select_n %1$s padt4">
						%2$s
					</select>
				</div>
				<div class="form-group fl marr3">
					<label class="sr-only" for="search01">검색어</label>
					<input type="text" class="form-control borderr1 %3$s" id="search01" placeholder="검색어 입력">
				</div>
				<div class="form-group fl marr3">
					<a href="javascript:void(0)"  class="btn btn-default dib %4$s">검색</a>
				</div>
			</div>',
			$argArray['selectId'], $option, $argArray['selectValId'], $argArray['searchBtnId']
		);
		return $retVal;
	 }

	 function nSearchBarBType($argArray = array()){
		$option = '';
		$mDom = '';
		foreach($argArray['optionItems'] as $key=>$val){
			$option .= sprintf('<option value="%s">%s</option>',
				$this->CI->nurl->encrypt($key), $val
			);
		}

		/* select 부분 추가*/
		if(@is_array($argArray['group'])){
			foreach($argArray['group'] as $keygroup => $valgroup){
				$mDom .= sprintf('
					<optgroup label="%s">',
					$valgroup['outgroup']
				);
				foreach($valgroup['items'] as $keyItem => $valItem){
					$mDom .= sprintf('<option value="%s">%s</option>',
						$keyItem, $valItem
					);
				}
				$mDom .= sprintf('</optgroup>');
			}
		}
		if(@is_array($argArray['items'])) {
			foreach($argArray['items'] as $keyItem => $valItem){
				$mDom .= sprintf('<option value="%s" >%s</option>',
					$keyItem, $valItem
				);
			}
		}
		$select = sprintf('
			<select class="selectpicker" name="%s">
				%s
			</select>
			',
			@$this->CI->nurl->encrypt($argArray['groupId']),
			$mDom
		);


		$retVal = sprintf('
			<div class="fr">
				<div class="form-group fl marr3">
					<select class="form-control borderr1 curp select_n padt4 %1$s">
						<option value="">선택하세요</option>
						%2$s
					</select>
				</div>
				<div class="form-group fl marr3">
					<label class="sr-only" for="search01">검색어</label>
					<input type="text" class="form-control borderr1 %3$s" id="search01" placeholder="검색어 입력">
				</div>
				<div class="form-group fl marr3 %8$s" style="display:none">
					%5$s
				</div>
				<div class="form-group fl marr3">
					<a href="javascript:void(0)"  class="btn btn-default dib %4$s">검색</a>
				</div>
				<div class="form-group fl marr3 %7$s" style="display:none">
					<a href="javascript:void(0)"  class="btn btn-default dib %6$s">검색</a>
				</div>
			</div>',
			$argArray['selectId'],
			$option,
			$argArray['selectValId'],
			$argArray['searchBtnId'],
			$select,
			$argArray['searchBtnId02'],
			$argArray['searchBtnWrapperId02'],
			$argArray['selectWrapperId01']
		);
		return $retVal;
	 }

}

/* End of file */